package com.stars.home.utils

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.home.R
import com.stars.home.inte.ChoiceListData
import com.stars.home.inte.ChoseTimeData
import com.stars.home.view.TimeMileView

/**
 * 时间段 时分秒
 *
 *on 2020/11/21
 */
object ChoseMileTime {


    fun show(context: Context,star_time:String?,end_time:String?, view: View, choiceCallData: ChoseTimeData) {

        var hourStart = ""
        var minuteStart = ""
        var secondStart=""

        var hourEnd=""
        var minuteEnd=""
        var secondEnd=""

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_mile_chose, null)
        val ll: LinearLayout = layout.findViewById(R.id.ll)
        val tvCancel: TextView = layout.findViewById(R.id.tv_cancel)
        val tvSure: TextView = layout.findViewById(R.id.tv_sure)
        val wheelView: TimeMileView = layout.findViewById(R.id.wheel_view)

       // wheelView.setData(day, daytime)



        hourStart = wheelView.getTimeTwo()//时间
        hourEnd = wheelView.getTimeThree()//时间

        minuteStart=wheelView.getMinuteStart()
        minuteEnd=wheelView.getMinuteEnd()

        secondStart=wheelView.getSendStart()
        secondEnd=wheelView.getSendEnd()



        //设置已经编辑的默认时间
        if(null!=star_time&&!TextUtils.isEmpty(star_time)){


            var ss=star_time.split(":")
            hourStart=ss[0]
            minuteStart=ss[1]
            secondStart=ss[2]

            wheelView.setTimeStart(star_time)

        }
        if(null!=end_time&&!TextUtils.isEmpty(end_time)){



            var ss=end_time.split(":")
            hourEnd=ss[0]
            minuteEnd=ss[1]
            secondEnd=ss[2]

            wheelView.setTimeEnd(end_time)
        }


        //--------
        wheelView.setCallBack(object : ChoiceListData {
            override fun onRestuse(code: String?, name: String?) {

                if (code == "2") {//开始时
                    hourStart = name!!
                }

                if (code == "3") {//结束 时
                    hourEnd = name!!
                }

                if (code == "6") {//开始 分

                    minuteStart= name!!
                }

                if (code == "7") {//结束 分

                    minuteEnd= name!!
                }

                if (code == "8") {//开始 秒

                    secondStart= name!!
                }

                if (code == "9") {//结束 秒

                    secondEnd= name!!
                }

            }
        })

        tvSure.onClick {



            if (null != choiceCallData) {
                choiceCallData!!.onRestuse("$hourStart:$minuteStart:$secondStart", "$hourEnd:$minuteEnd:$secondEnd")
            }

            mNormalPopup?.dismiss()
        }


        tvCancel.onClick {
            mNormalPopup?.dismiss()
        }

        ll.onClick {

            mNormalPopup?.dismiss()
        }

        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_AUTO)
        mNormalPopup.show(view)


    }


}
